package com.zh.note.demo.javaSE;



import java.util.HashSet;
import java.util.Set;

/**
 * @Author: zhanghuan
 * @date: 2025/8/23 22:42
 * @description: create it
 *
 * ==对于基本类型看值是否相等，对于引用类型看内存地址是否相等
 * equals 看是否重写过。没有重写，默认是==；如果重写看具体实现
 */
public class EqualsDemo {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("abc");
        System.out.println(s1 == s2);   // false
        System.out.println(s1.equals(s2)); //true

        Set<String> set = new HashSet<>();
        set.add(s1);
        set.add(s2);
        System.out.println(set.size()); //1
        System.out.println(s1.hashCode() + "\t" + s2.hashCode());
        System.out.println("-------------------");
        Person p1 = new Person("abc");
        Person p2 = new Person("abc");
        System.out.println(p1 == p2);   //false
        System.out.println(p1.equals(p2)); //false
        Set<Person> personSet = new HashSet<>();
        personSet.add(p1);
        personSet.add(p2);

        System.out.println(personSet.size()); //2
        System.out.println(p1.hashCode() + "\t" + p2.hashCode());
    }

    static class Person {
        private Integer id;
        private String name;

        public Person(String name) {
            this.name = name;
        }
    }
}
